/**
 * Created by smp on 2017/5/3.
 */
var modelPath=ROOT_PATH+'/majorHazard/activityRecord/';
var PROBLE_TYPE=CONSTANT.DictionaryTypeDefConstants.PROBLE_TYPE;
var getSelectedInformation = function (data) {
    $("#keyDevice").textbox("setValue",data[0].devicePartName);
};
//初始化下拉树
$('#beCheckedUnit').combotree({
    url: ROOT_PATH+'/permission/department/getDepartmentTree.shtml?parentId=0'
});
//初始化下拉树
$('#beCheckedUnit').combotree({
    url: ROOT_PATH+'/permission/department/getDepartmentTree.shtml?parentId=0'
});
$("#rectificationUnit").combotree({
    url: ROOT_PATH+'/permission/department/getDepartmentTree.shtml?parentId=0'
});
$("#jobArea").combotree({
    url: ROOT_PATH+'/permission/department/getDepartmentTree.shtml?parentId=0'
});

var getSelectedUser = function (data) {
    var id=data[0].id;
    var realName=data[0].realName;
    $("#rectificationContact").val(id);
    $("#rectificationContactName").textbox("setValue",realName);

};

$('#rectificationContactName').textbox({
    required: true,
    icons: [{
        iconCls: 'icon-search',
        handler: function () {
            WindowManager.openLayer({
                type: 2,
                title: '添加整改(联络)人',
                area: ['90%', '70%'],
                content: ROOT_PATH + '/permission/user/simpleList.shtml?isMulti=0',
                callBack:getSelectedUser
            });
        }
    }],
    editable: false
});


$('#keyDevice').textbox({
    required: true,
    icons: [{
        iconCls: 'icon-search',
        handler: function () {
            WindowManager.openLayer({
                type: 2,
                title: '关键装置/要害部位',
                area: ['90%', '70%'],
                content: ROOT_PATH + '/majorHazard/informationRegistration/simpleList.shtml',
                callBack:getSelectedInformation
            });
        }
    }],
    editable: false
});
var getSelectedContractor=function (data) {
    data = data[0];
    $('#contractorName').textbox('setValue', data.name);
    $('#contractor').val(data.id);
};
$("#contractorName").textbox({
    required: true,
    icons: [{
        iconCls: 'icon-search',
        handler: function () {
            WindowManager.openLayer({
                type: 2,
                title: '承包商',
                area: ['90%', '70%'],
                content: ROOT_PATH + '/contractor/contractorInfo/simpleList.shtml',
                callBack:getSelectedContractor
            });
        }
    }],
    editable: false
});
//表单提交
$('#btn_save').on('click',function () {
    var index=layer.confirm('是否确定保存？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        $('#form1').form('submit', {
            url: modelPath+'problem.shtml',

            onSubmit: function (param) {
                var isValid = $(this).form('validate');
                if (!isValid) {
                    layer.close(index);
                    layer.msg("请正确填写表单")
                }
                return $(this).form('validate');
            },
            success: function (data) {
                var result = $.parseJSON(data).result;
                if (result.success){
                    //$.messager.progress('close');	// 如果提交成功则隐藏进度条
                    layer.msg(result.message,{time:1000,icon:1},function () {
                       // parent.layer.close(parent.layer.getFrameIndex(window.name));
                        //$.getIframeWindow().$('#precautionList').datagrid('load');
                        WindowManager.closeLayer({
                            returnValue: result
                        });
                    });
                }else {
                    //$.messager.progress('close');	// 如果提交成功则隐藏进度条
                    parent.layer.msg(result.message,{time:1000,icon:2});
                }
            }
        });
    });

});

/**
 * 下发
 */
function editmonitor(id,result) {
    var taskName="活动记录"+$("#keyDevice").textbox("getValue")+"装置"+"问题整改";
    var objTask={};//参数对象
    var taskArr=[];//任务数据数组
        var obj={
            taskId:id,//业务Id
            taskName:taskName,//任务名称
            taskSource:'活动记录问题整改'//来源模块
        };
        taskArr.push(obj);

    objTask.task=taskArr;//参数 对象中的键值可以不是task 但只有一个
    WindowManager.openLayer({//任务窗口
        type: 2,
        title: '批量任务下发',
        area: ['1150px', '70%'],
        content: ROOT_PATH + '/task/addTask.shtml',
        data:objTask,//参数
        callBack:function(data){//窗口关闭后的回调函数 data=true
            if (data){
                //$.messager.progress('close');	// 如果提交成功则隐藏进度条
                layer.msg("下发完成",{time:1000,icon:1},function () {
                   // parent.layer.close(parent.layer.getFrameIndex(window.name));
                   // $.getIframeWindow().$('#precautionList').datagrid('load');
                    WindowManager.closeLayer({
                        returnValue: result
                    });
                });
            }
        }
    });
}
$("#btn_issued").click(function(){
    var index=layer.confirm('是否确定下发？', {
        btn: ['确定','取消'] //按钮
    }, function(){
        $('#form1').form('submit', {
            url: modelPath+'problem.shtml',

            onSubmit: function (param) {
                var isValid = $(this).form('validate');
                if (!isValid) {
                    layer.close(index);
                    layer.msg("请正确填写表单")
                }
                return $(this).form('validate');
            },
            success: function (data) {
                var result = $.parseJSON(data).result;
                var id=$.parseJSON(data).id;
                if (result.success){
                    editmonitor(id,result);
                }else {
                    //$.messager.progress('close');	// 如果提交成功则隐藏进度条
                    parent.layer.msg(result.message,{time:1000,icon:2});
                }
            }
        });
    });
});


$(function(){
    $('#specialtyClassification').combobox({
        url:ROOT_PATH+'/permission/dictionary/queryDictionaryByDictionaryTypeCode.shtml?code='+PROBLE_TYPE,
        required:true,
        editable:false,
        valueField:'id',
        textField:'text',
        onSelect: function(record){
            $('#problemClassification').combobox("clear");
            $('#problemClassification').combobox({
                url:ROOT_PATH+'/permission/dictionary/queryDictionaryByDatas.shtml?data1='+record.code,
                required:true,
                editable:false,
                valueField:'id',
                textField:'text'
            });
        }
    });
    $('#unitProperty').combobox({
        onSelect: function(param){
            $("#superiorDepartment").combobox("clear");
                var datasss;
                if(param.value=="存续") {
                    datasss=[
                        {'id':'石油工程公司','value':'石油工程公司','text':'石油工程公司'},
                        {'id':'矿区(社区)管理部','value':'矿区(社区)管理部','text':'矿区(社区)管理部'},
                        {'id':'资产公司','value':'资产公司','text':'资产公司'}];
                }else{
                    datasss=[
                        {'id':'炼化公司(集团)股份有限公司','value':'炼化公司(集团)股份有限公司','text':'炼化公司(集团)股份有限公司'},
                        {'id':'油田事业部','value':'油田事业部','text':'油田事业部'},
                        {'id':'炼油事业部','value':'炼油事业部','text':'炼油事业部'},
                        {'id':'化学事业部','value':'化学事业部','text':'化学事业部'},
                        {'id':'油品销售事业部','value':'油品销售事业部','text':'油品销售事业部'}];
                }
            $('#superiorDepartment').combobox("loadData",datasss);
        }
    });


    $("#isContractor").combobox({
        onSelect:function(param){
            if(param.value==0){
                $(".contractor").css('display','none');
                $(".beCheckedUnit").css('display','inline');
                $("#beCheckedUnit").textbox({"required":true});
                $("#contractorName").textbox({"required":false});
                $("#contactPerson").textbox({"required":false});
                $("#jobArea").textbox({"required":false});
                $("#jobName").textbox({"required":false});
               /* $("#contractorName").textbox("setValue",null);
                $("#contractor").val(null);
                $("#contactPerson").textbox("setValue",null);
                $("#jobArea").textbox("setValue",null);
                $("#jobName").textbox("setValue",null);*/
            }else {
                $(".beCheckedUnit").css('display','none');
                $(".contractor").css('display','inline');
                $("#beCheckedUnit").textbox({"required":false});
                $("#contractorName").textbox({"required":true});
                $("#contactPerson").textbox({"required":true});
                $("#jobArea").textbox({"required":true});
                $("#jobName").textbox({"required":true});
            }
        }
    });
});
